/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021 并保留所有权利。
 * Copyright 2021, YCHS Information&Science Techology Co.,Ltd,
 * All right reserved.
 */
package com.ychs.lawyerback.controller;

import com.ychs.lawyerback.annotation.RequiredPermission;
import com.ychs.lawyerback.common.R;
import com.ychs.lawyerback.dto.MenuOptDTO;
import com.ychs.lawyerback.dto.MenuQueryDTO;
import com.ychs.lawyerback.service.MenuService;
import com.ychs.lawyerback.vo.MenuQueryVo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author mayujing
 * @version 1.0
 */

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/getMenusByUserId/{userId}")
    public R<?> getMenusByUserId(@PathVariable Long userId) {
        List<MenuQueryVo> menus=  menuService.getMenusByUserId(userId);
        return R.success(menus);
    }

    @GetMapping("/list")
    public R<?> getMenuList(MenuQueryDTO queryDTO) {
        Map<String, Object> result = menuService.getMenuList(queryDTO);
        return R.success(result);
    }

    @GetMapping("/tree")
    public R<?> getMenuTree() {
        List<MenuQueryVo> menuTree = menuService.getMenuTree();
        return R.success(menuTree);
    }

    @GetMapping("menuId/{menuId}")
    public R<?> getMenuById(@PathVariable Long menuId) {
        MenuQueryVo menu = menuService.getMenuById(menuId);
        return R.success(menu);
    }

    @PostMapping("/add")
    @RequiredPermission("system:menu:add")
    public R<?> addMenu(@Valid @RequestBody MenuOptDTO menuOptDTO) {
        int res = menuService.addMenu(menuOptDTO);
        return res > 0 ? R.success() : R.fail();
    }

    @PutMapping("/edit")
    @RequiredPermission("system:menu:edit")
    public R<?> updateMenu(@Valid @RequestBody MenuOptDTO menuOptDTO) {
        int res = menuService.updateMenu(menuOptDTO);
        return res > 0 ? R.success() : R.fail();
    }

    @DeleteMapping("/delete/{ids}")
//    @RequiredPermission("system:menu:delete")
    public R<?> deleteMenu(@PathVariable String ids) {
        int res = menuService.deleteMenu(ids);
        return res > 0 ? R.success() : R.fail();
    }
}
